package org.celery.modules.shop.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.celery.modules.shop.entity.ShopDept;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.celery.modules.shop.entity.ShopUser;
import org.celery.modules.shop.vo.ShopUserVO;
import org.celery.modules.shop.wrapper.ShopUserWrapper;
import org.celery.modules.shop.service.IShopUserService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 店铺用户关联表 控制器
 *
 * @author Celery
 * @since 2023-09-11
 */
@RestController
@AllArgsConstructor
@RequestMapping("celery-shop-user/shop-user")
@Api(value = "店铺用户关联表", tags = "店铺用户关联表接口")
public class ShopUserController extends BladeController {

	private final IShopUserService shopUserService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入shopUser")
	public R<ShopUserVO> detail(ShopUser shopUser) {
		ShopUser detail = shopUserService.getOne(Condition.getQueryWrapper(shopUser));
		return R.data(ShopUserWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 店铺用户关联表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入shopUser")
	public R<IPage<ShopUserVO>> list(ShopUser shopUser, Query query) {
		IPage<ShopUser> pages = shopUserService.page(Condition.getPage(query), Condition.getQueryWrapper(shopUser));
		return R.data(ShopUserWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 店铺用户关联表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入shopUser")
	public R<IPage<ShopUserVO>> page(ShopUserVO shopUser, Query query) {
		IPage<ShopUserVO> pages = shopUserService.selectShopUserPage(Condition.getPage(query), shopUser);
		return R.data(pages);
	}

	/**
	 * 新增 店铺用户关联表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入shopUser")
	public R save(@Valid @RequestBody ShopUser shopUser) {
		return R.status(shopUserService.save(shopUser));
	}

	/**
	 * 修改 店铺用户关联表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入shopUser")
	public R update(@Valid @RequestBody ShopUser shopUser) {
		return R.status(shopUserService.updateById(shopUser));
	}

	/**
	 * 新增或修改 店铺用户关联表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入shopUser")
	public R submit(@Valid @RequestBody ShopUser shopUser) {
		return R.status(shopUserService.saveOrUpdate(shopUser));
	}

	
	/**
	 * 删除 店铺用户关联表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(shopUserService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 保存列表
	 */
	@PostMapping("/save-list")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "保存列表")
	public R saveList(@ApiParam(value = "用户id", required = true) @RequestParam Long userId,
					  @ApiParam(value = "店铺ids", required = true) @RequestParam String shopIds) {
		List<ShopUser> shopDeptList = shopUserService.list(Wrappers.<ShopUser>lambdaQuery()
				.eq(ShopUser::getUserId, userId)
		);

		List<String> shopIdList = shopDeptList.stream().map(item -> String.valueOf(item.getShopId())).collect(Collectors.toList());
		List<String> shopIdNewList = Func.toStrList(shopIds);

		List<String> addIdList = new ArrayList<>();
		shopIdNewList.forEach(shopId -> {
			if (!shopIdList.contains(shopId)) {
				addIdList.add(shopId);
			}
		});

		List<String> removeIdList = new ArrayList<>();
		shopIdList.forEach(shopId -> {
			if (!shopIdNewList.contains(shopId)) {
				removeIdList.add(shopId);
			}
		});

		if (Func.isNotEmpty(removeIdList)) {
			shopUserService.remove(Wrappers.<ShopUser>lambdaQuery().eq(ShopUser::getUserId, userId).in(ShopUser::getShopId, removeIdList));
		}
		if (Func.isNotEmpty(addIdList)) {
			List<ShopUser> addList = new ArrayList<>();
			addIdList.forEach(id -> {
				ShopUser shopUser = new ShopUser();
				shopUser.setUserId(userId);
				shopUser.setShopId(Long.valueOf(id));
				addList.add(shopUser);
			});
			shopUserService.saveOrUpdateBatch(addList);
		}
		return R.status(true);
	}

	
}
